package cn.skyquant.quant4j.jforex.strategy;

import cn.skyquant.quant4j.sdk.util.log.LogUtil;
import com.dukascopy.api.*;
import com.dukascopy.api.feed.ITickFeedListener;
import com.google.common.collect.Sets;

import java.util.*;

/**
 * 宙斯
 * 提供quant4j数据服务的策略
 * @author harley-dog
 */
public class Zeus implements IStrategy , ITickFeedListener {


    public static Set<Instrument> instruments = Sets.newHashSet(
            Instrument.AUDCAD,
            Instrument.AUDUSD,
            Instrument.CADJPY,
            Instrument.CHFJPY,
            Instrument.EURAUD,
            Instrument.EURCAD,
            Instrument.EURCHF,
            Instrument.EURJPY,
            Instrument.EURUSD,
            Instrument.GBPCAD,
            Instrument.GBPJPY,
            Instrument.GBPUSD,
            Instrument.NZDCAD,
            Instrument.NZDUSD,
            Instrument.USDCAD,
            Instrument.USDCHF,
            Instrument.USDJPY,
            Instrument.XAUUSD);
    /*基本参数*/
    private IHistory history;
    private IContext context;
    private Map<Instrument,ITick> tickMap = new HashMap<>();
    private static List<IBar> empty = new ArrayList<>();
    public boolean isStart(){
        return context==null || !context.isStopped();
    }
    public ITick getITick(Instrument instrument){
        return tickMap.get(instrument);
    }
    public long getContextTime(){
        return context==null?0:context.getTime();
    }

    public List<IBar> getBars(Instrument instrument, Period period, OfferSide side, Filter filter, long from, long to) throws JFException{
        long realfrom = history.getBarStart(period,from);
        long realto = history.getBarStart(period,to);
        return history==null?empty:history.getBars(instrument,period,side,filter,realfrom,realto);
    }
    public IAccount getAccount(){
        return context==null?null:context.getAccount();
    }
    public Set<Map.Entry<Instrument,ITick>> getAllTick(){
        return tickMap.entrySet();
    }

    @Override
    public void onStart(IContext context) throws JFException {
        this.history = context.getHistory();
        this.context = context;
        this.context.setSubscribedInstruments(instruments);
        for(Instrument instrument:instruments){
            context.subscribeToTicksFeed(instrument,this);
        }
        LogUtil.info("account:"+context.getAccount().getAccountId()+":"+context.getAccount().getUserName());
        LogUtil.info("Zeus start");
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) {
        tickMap.put(instrument,tick);
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    @Override
    public void onStop() throws JFException {
        this.history = null;
        this.context  = null;
        tickMap.clear();
        LogUtil.info("Zeus stop");
    }
}
